<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 400px;
            min-height: 100px;
            border: 2px dotted red;
        }

        div span {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 1px solid red;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            margin: 10px;
        }

        div span:last-child {
            border: 1px solid green;
        }
    </style>
</head>

<body>
    <button>获取彩票信息</button>
    <div></div>
    <script>
        var oBtn = document.querySelector('button');
        var oDiv = document.querySelector('div');
        oBtn.onclick = function () {
            //6个红球
            var ball = getRandom(6, 1, 33);
            ball.push(...getRandom(1, 1, 16));
            console.log(ball);
            history.pushState(ball, "");
            writeHTML(ball);
        }

        window.onpopstate = function (ev) {
            console.log(ev);
            writeHTML(ev.state);
        }

        function writeHTML(ball) {
            var str = "";
            if (ball == null) {
                oDiv.innerHTML = "历史记录已经查找完";
                return;
            }
            for (let i = 0; i < ball.length; i++) {
                str += `<span>${ball[i]}</span>`;
            }
            oDiv.innerHTML = str;
        }


        function getRandom(count, start, end) {
            var numArr = [];
            for (var i = 0; i < count; i++) {
                var num1 = Math.floor(Math.random() * (end - start) + start);
                if (numArr.indexOf(num1) !== -1) {
                    i--;//重复了，球重新获取
                } else {
                    numArr.push(num1);
                }
            }
            return numArr;
        }

    </script>
</body>

</html>